<?php

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\InfluencerController;
use App\Http\Controllers\Api\ReportController;
use App\Http\Controllers\MjzjCrawlerController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
*/

// 公开路由
Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
Route::get('/reports/count', [ReportController::class, 'count']);
// 认证状态检查（用于插件同步状态检查）
Route::get('/auth-status', function (Illuminate\Http\Request $request) {
    $isAuthenticated = \Illuminate\Support\Facades\Auth::check();
    $user = null;
    $needsTokenRefresh = false;
    
    if ($isAuthenticated) {
        $user = \Illuminate\Support\Facades\Auth::user();
        
        // 检查是否需要刷新token（例如最后一次刷新超过7天）
        $lastTokenRefresh = $request->session()->get('last_token_refresh', 0);
        $needsTokenRefresh = (time() - $lastTokenRefresh) > (7 * 24 * 60 * 60);
    }
    
    return response()->json([
        'authenticated' => $isAuthenticated,
        'user' => $user ? [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
            'email_verified_at' => $user->email_verified_at,
        ] : null,
        'needsTokenRefresh' => $needsTokenRefresh,
        'timestamp' => now()->timestamp
    ]);
});


// 测试路由
Route::get('test', function () {
    return response()->json(['message' => 'Hello, World!']);
});

// 需要认证的路由
Route::middleware('auth:sanctum')->group(function () {
    // 登出
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::get('/user', [AuthController::class, 'user']);

    // 举报相关路由
    Route::post('/reports', [ReportController::class, 'store']);
    Route::get('/reports', [ReportController::class, 'index']);
    // Route::get('/reports', function () {
    //     var_dump('test');
    // });


    // 网红相关路由
    Route::get('/influencers/{influencer}/score', [InfluencerController::class, 'score']);
    Route::get('/influencers/{influencer}/reports', [InfluencerController::class, 'reports']);

    // 卖家之家爬虫相关路由
    Route::prefix('mjzj')->group(function () {
        Route::post('/crawl', [MjzjCrawlerController::class, 'crawl']);
        Route::post('/crawl-batch', [MjzjCrawlerController::class, 'crawlBatch']);
        Route::get('/stats', [MjzjCrawlerController::class, 'stats']);
        Route::get('/reviewers', [MjzjCrawlerController::class, 'reviewers']);
        Route::get('/reviewers/{id}', [MjzjCrawlerController::class, 'show']);
        Route::delete('/reviewers/{id}', [MjzjCrawlerController::class, 'destroy']);
    });
}); 